package com.baihan.angus.app.game.busshow.adps;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.BaseApplication;
import com.baihan.angus.app.game.basic.base.BaseAdp;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.BbsResponse;
import com.baihan.angus.app.game.common.util.DateUtils;
import com.baihan.angus.app.game.common.util.ImageLoader;
import com.baihan.angus.app.game.common.util.media.DefMediaInfo;
import com.baihan.angus.app.game.common.util.media.IMediaInfo;
import com.baihan.angus.app.game.common.util.media.MediaType;
import com.baihan.angus.app.game.common.util.media.MediaTypeUtil;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.ArrayList;
import java.util.List;

public class FollowAdp extends BaseAdp<BbsResponse.DataX.Data, FollowAdp.ViewHolder> {

    private OnItemClickListener mOnItemClickListener;
    private OnMediaAdpCallback mOnMediaAdpCallback;

    @Override
    protected RecyclerView.ViewHolder createYourViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_follow, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void bindYourViewHolder(@NonNull ViewHolder holder, int position) {
        ViewHolder vh = (ViewHolder) holder;

        BbsResponse.DataX.Data data = getItem(position);

        if (null != data) {
            vh.tvName.setText(data.getUser().getNickname());
            vh.tvTime.setText(DateUtils.getDate1(data.getCreate_time()));
            vh.tvTag.setText("#" + data.getTags() + "#");
            vh.tvContent.setText(data.getContent());
            vh.tvLikes.setText(data.getLikes() + "");
            vh.tvCollects.setText(data.getRecommend() + "");
            vh.tvComments.setText(data.getRecommend() + "");

            ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                    vh.ivAvatar, data.getUser().getAvatar(), R.mipmap.ic_launcher, 0);

            if (null == data.getPics() && data.getPics().size() <= 0){
                vh.llImagesPreview.setVisibility(View.GONE);
            } else {
                vh.llImagesPreview.setVisibility(View.VISIBLE);
                List<BbsResponse.DataX.Data.Pics> picList = data.getPics();
                List<IMediaInfo> list = new ArrayList<>();
                for (int k = 0; k < data.getPics().size(); k++) {
                    list.add(new DefMediaInfo(picList.get(k).getUrl()));
                    if (k == 0) {
                        vh.ivVideoPlay1.setVisibility(MediaTypeUtil.getMediaType(picList.get(k).getUrl()) == MediaType.VIDEO ? View.VISIBLE : View.INVISIBLE);
                    } else if (k == 1) {
                        vh.ivVideoPlay2.setVisibility(MediaTypeUtil.getMediaType(picList.get(k).getUrl()) == MediaType.VIDEO ? View.VISIBLE : View.INVISIBLE);
                    } else if (k == 2) {
                        vh.ivVideoPlay3.setVisibility(MediaTypeUtil.getMediaType(picList.get(k).getUrl()) == MediaType.VIDEO ? View.VISIBLE : View.INVISIBLE);
                    }
                }

                if (picList.size() == 1) {
                    vh.cl1.setVisibility(View.VISIBLE);
                    vh.cl2.setVisibility(View.INVISIBLE);
                    vh.cl3.setVisibility(View.INVISIBLE);

                    ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                            vh.ivPic1, picList.get(0).getUrl(), R.mipmap.ic_launcher, 0);

                    vh.cl1.setOnClickListener(v -> {
                        if (null != mOnMediaAdpCallback) {
                            mOnMediaAdpCallback.preview(0, list);
                        }
                    });
                } else if (picList.size() == 2) {
                    vh.cl1.setVisibility(View.VISIBLE);
                    vh.cl2.setVisibility(View.VISIBLE);
                    vh.cl3.setVisibility(View.INVISIBLE);

                    ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                            vh.ivPic1, picList.get(0).getUrl(), R.mipmap.ic_launcher, 0);

                    ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                            vh.ivPic2, picList.get(1).getUrl(), R.mipmap.ic_launcher, 0);

                    vh.cl1.setOnClickListener(v -> {
                        if (null != mOnMediaAdpCallback) {
                            mOnMediaAdpCallback.preview(0, list);
                        }
                    });

                    vh.cl2.setOnClickListener(v -> {
                        if (null != mOnMediaAdpCallback) {
                            mOnMediaAdpCallback.preview(1, list);
                        }
                    });
                } else if (picList.size() >= 3) {
                    vh.cl1.setVisibility(View.VISIBLE);
                    vh.cl2.setVisibility(View.VISIBLE);
                    vh.cl3.setVisibility(View.VISIBLE);

                    ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                            vh.ivPic1, picList.get(0).getUrl(), R.mipmap.ic_launcher, 0);

                    ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                            vh.ivPic2, picList.get(1).getUrl(), R.mipmap.ic_launcher, 0);

                    ImageLoader.loadImage(BaseApplication.getApplication().getApplicationContext(),
                            vh.ivPic3, picList.get(2).getUrl(), R.mipmap.ic_launcher, 0);
                }

                vh.cl1.setOnClickListener(v -> {
                    if (null != mOnMediaAdpCallback) {
                        mOnMediaAdpCallback.preview(0, list);
                    }
                });

                vh.cl2.setOnClickListener(v -> {
                    if (null != mOnMediaAdpCallback) {
                        mOnMediaAdpCallback.preview(1, list);
                    }
                });

                vh.cl3.setOnClickListener(v -> {
                    if (null != mOnMediaAdpCallback) {
                        mOnMediaAdpCallback.preview(2, list);
                    }
                });
            }
        }

        vh.llFollowItem.setOnClickListener(v -> {
            mOnItemClickListener.onItemClick(position);
        });

        vh.ivShare.setOnClickListener(v -> {
            mOnItemClickListener.onShare(position);
        });

        vh.tvLikes.setOnClickListener(v -> {
            mOnItemClickListener.onLike(position);
        });

        vh.tvCollects.setOnClickListener(v -> {
            mOnItemClickListener.onCollect(position);
        });

        vh.tvComments.setOnClickListener(v -> {
            mOnItemClickListener.onComment(position);
        });
    }

    @Override
    protected int getEmptyView() {
        return 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView ivVideoPlay1, ivVideoPlay2, ivVideoPlay3, ivShare;
        QMUIRadiusImageView ivAvatar, ivPic1, ivPic2, ivPic3;
        TextView tvContent, tvName, tvTime, tvTag, tvComments, tvCollects, tvLikes;
        ConstraintLayout cl1, cl2, cl3;
        LinearLayout llFollowItem, llImagesPreview;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            tvContent = itemView.findViewById(R.id.tvContent);
            tvName = itemView.findViewById(R.id.tvName);
            tvTime = itemView.findViewById(R.id.tvTime);
            tvTag = itemView.findViewById(R.id.tvTag);
            tvComments = itemView.findViewById(R.id.tvComments);
            tvCollects = itemView.findViewById(R.id.tvCollects);
            tvLikes = itemView.findViewById(R.id.tvLikes);
            cl1 = itemView.findViewById(R.id.cl1);
            cl2 = itemView.findViewById(R.id.cl2);
            cl3 = itemView.findViewById(R.id.cl3);
            ivPic1 = itemView.findViewById(R.id.ivPic1);
            ivPic2 = itemView.findViewById(R.id.ivPic2);
            ivPic3 = itemView.findViewById(R.id.ivPic3);
            ivShare = itemView.findViewById(R.id.ivShare);
            ivVideoPlay1 = itemView.findViewById(R.id.ivVideoPlay1);
            ivVideoPlay2 = itemView.findViewById(R.id.ivVideoPlay2);
            ivVideoPlay3 = itemView.findViewById(R.id.ivVideoPlay3);
            llImagesPreview = itemView.findViewById(R.id.llImagesPreview);
            llFollowItem = itemView.findViewById(R.id.llFollowItem);
        }
    }

    public void setOnMediaAdpCallback(OnMediaAdpCallback mOnMediaAdpCallback) {
        this.mOnMediaAdpCallback = mOnMediaAdpCallback;
    }

    public interface OnMediaAdpCallback {
        void preview(int index, List<IMediaInfo> list);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onLike(int position);

        void onShare(int position);

        void onCollect(int position);

        void onComment(int position);
    }
}
